package com.pureut.system.service.impl;


import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.DrviceParameter;
import com.pureut.system.domain.DrviceRecord;
import com.pureut.system.domain.dto.DrviceParameterDto;
import com.pureut.system.domain.dto.DrviceRecordDto;
import com.pureut.system.domain.dto.GetDrviceInfoDto;
import com.pureut.system.domain.vo.DrviceParameterVo;
import com.pureut.system.domain.vo.DrviceRecordVo;
import com.pureut.system.mapper.DrviceRecordMapper;
import com.pureut.system.mapper.DrviceparamterMapper;
import com.pureut.system.service.DrviceRecordService;
import com.pureut.system.service.ISysMidSplitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/2/13  11:22
 */

@Service
public class DrviceRecordServiceImpl implements DrviceRecordService {

    /**
     * 设备台账信息
     */
    @Resource
    private DrviceRecordMapper drviceRecordMapper;

    /**
     * 编码生成配置
     */
    @Resource
    private ISysMidSplitService sysMidSplitService;

    /**
     * 工艺参数
     */
    @Resource
    DrviceparamterMapper drviceparamterMapper;

    /**
     * 设备台账
     * @param drviceRecordVo
     * @return
     */
    @Override
    public List<DrviceRecordDto> getList(DrviceRecordVo drviceRecordVo) {
        List<DrviceRecordDto> drviceList = drviceRecordMapper.getDrviceList(drviceRecordVo);

        List<SysDictData> statusArray = DictUtils.getDictCache("drvice_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<SysDictData> typeArray = DictUtils.getDictCache("drvice_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (DrviceRecordDto drviceRecordDto : drviceList) {
            drviceRecordDto.setDrviceTypeDict(drviceRecordDto.getDrviceType());
//            drviceRecordDto.setDrviceStatusDict(drviceRecordDto.getDrviceStatus());
            drviceRecordDto.setDrviceType(typeMap.get(drviceRecordDto.getDrviceType()));
            drviceRecordDto.setDrviceStatusDict(statusMap.get(drviceRecordDto.getDrviceStatus()));
        }
        return drviceList;
    }

    /**
     * 新增设备台账
     * @param drviceRecordVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int drviceAdd(DrviceRecordVo drviceRecordVo) throws Exception {
        DrviceRecord drviceRecord = new DrviceRecord();
        BeanUtils.copyProperties(drviceRecordVo,drviceRecord);
        drviceRecord.setDeptId(SecurityUtils.getDeptId());
        String sb = sysMidSplitService.splitStr("SB");
        drviceRecord.setDrviceCode(sb);
        int insertDrvice = drviceRecordMapper.insertDrvice(drviceRecord);

        //新增成功开始新增工艺参数
        if (insertDrvice>0){
            List<DrviceParameterVo> list = drviceRecordVo.getList();
            List<DrviceParameter> parameterList = new ArrayList<>();

            if (list!=null){
                DrviceParameter drviceParameter = new DrviceParameter();
                for (DrviceParameterVo drviceParameterVo : list) {
                    BeanUtils.copyProperties(drviceParameterVo,drviceParameter);
                    drviceParameter.setDrviceCode(sb);
                    parameterList.add(drviceParameter);

                }

                insertDrvice=drviceparamterMapper.insertParameter(parameterList);
                if (insertDrvice==0){
                    throw new GlobalException("新增失败");
                }
            }

        }
        return insertDrvice;
    }

    /**
     * 设备台账修改
     * @param drviceRecordVo
     * @return
     */
    @Override
    public int drviceEdit(DrviceRecordVo drviceRecordVo) {
        DrviceRecordDto drviceRecordDto = drviceRecordMapper.viewRecord(drviceRecordVo.getId());
        String drviceCode = drviceRecordDto.getDrviceCode();
        DrviceRecord drviceRecord = new DrviceRecord();
        BeanUtils.copyProperties(drviceRecordVo,drviceRecord);

        int updateDrvice = drviceRecordMapper.updateDrvice(drviceRecord);
        //如果设备信息修改成功
        if (updateDrvice>0){
            //删除设备明细信息
            int i = drviceparamterMapper.removeDrviceParamter(drviceCode);
            //添加设备明细信息
            List<DrviceParameterVo> list = drviceRecordVo.getList();
            List<DrviceParameter> drviceParameterList =new ArrayList<>();
            for (DrviceParameterVo drviceParameterVo : list) {
                DrviceParameter drviceParameter = new DrviceParameter();
                BeanUtils.copyProperties(drviceParameterVo,drviceParameter);
                drviceParameter.setDrviceCode(drviceCode);
                drviceParameter.setDrviceAddedit(drviceParameterVo.getDrviceAddedit());
                drviceParameter.setDrviceUnit(drviceParameterVo.getDrviceUnit());
                drviceParameterList.add(drviceParameter);
            }
            drviceparamterMapper.insertParameter(drviceParameterList);
        }
        return updateDrvice;
    }

    /**
     * 查看设备台账信息
     * @param id
     * @return
     */
    @Override
    public DrviceRecordDto view(long id) {
        //查询设备信息
        DrviceRecordDto drviceRecordDto=drviceRecordMapper.viewRecord(id);
        List<SysDictData> statusArray = DictUtils.getDictCache("drvice_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<SysDictData> typeArray = DictUtils.getDictCache("drvice_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        drviceRecordDto.setDrviceTypeDict(drviceRecordDto.getDrviceType());
        drviceRecordDto.setDrviceStatusDict(drviceRecordDto.getDrviceStatus());
        drviceRecordDto.setDrviceType(typeMap.get(drviceRecordDto.getDrviceType()));
        drviceRecordDto.setDrviceStatus(statusMap.get(drviceRecordDto.getDrviceStatus()));

        //获取设备编码

        String drviceCode = drviceRecordDto.getDrviceCode();
        //根据设备编码查出工艺参数
        List<DrviceParameterDto> drviceParameterDtoList = drviceparamterMapper.getDrviceParametr(drviceCode);
        drviceRecordDto.setList(drviceParameterDtoList);
        return drviceRecordDto;
    }

    /**
     * 删除设备台账信息
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(String ids) {
        String[] drviceIds = ids.split(",");

        int delete=0;

        for (String deviceId : drviceIds) {
            DrviceRecordDto drviceRecordDto = drviceRecordMapper.viewRecord(Long.parseLong(deviceId));
            String a=drviceRecordDto.getDrviceCode();
            delete = drviceparamterMapper.removeDrviceParamter(drviceRecordDto.getDrviceCode());
        }

        if (delete>0){
            drviceRecordMapper.removeByIds(drviceIds);
        }

        return delete;
    }

    /**
     * 根据线别编码查询设备信息
     * @param lineCode
     * @return
     */
    @Override
    public DrviceRecordDto getLine(String lineCode) {
        DrviceRecordDto drviceRecordDto=drviceRecordMapper.getLine(lineCode);
        List<SysDictData> statusArray = DictUtils.getDictCache("drvice_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<SysDictData> typeArray = DictUtils.getDictCache("drvice_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        drviceRecordDto.setDrviceTypeDict(drviceRecordDto.getDrviceType());
        drviceRecordDto.setDrviceStatusDict(drviceRecordDto.getDrviceStatus());
        drviceRecordDto.setDrviceType(typeMap.get(drviceRecordDto.getDrviceType()));
        drviceRecordDto.setDrviceStatus(statusMap.get(drviceRecordDto.getDrviceStatus()));
        return drviceRecordDto;
    }

    /**
     * 根据设备编码查出设备名称，设备类型，属性列表
     * @param drviceCode
     * @return
     */
    @Override
    public GetDrviceInfoDto getDrviceInfo(String drviceCode) {
        GetDrviceInfoDto drviceInfo = drviceRecordMapper.getDrviceInfo(drviceCode);
        List<SysDictData> typeArray = DictUtils.getDictCache("drvice_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        drviceInfo.setDrviceType(typeMap.get(drviceInfo.getDrviceType()));
        return drviceInfo;
    }


}
